package controller

import (
	"ci-control-backend/common/httpx"
	"ci-control-backend/model"
	"ci-control-backend/tools"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type UserManageController struct {
}

// NewUser 添加用户
func (*UserManageController) NewUser(c *httpx.Context) any {
	var req struct {
		Username string `json:"username"`
		Password string `json:"password"`
		Email    string `json:"email"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		return err
	}

	if req.Username == "" {
		return errors.New("请输入用户名")
	}
	if req.Password == "" {
		return errors.New("请输入密码")
	}
	if req.Email == "" {
		return errors.New("请输入邮箱")
	}

	loginUser, err := c.GetLoginUser()
	if err != nil {
		return err
	}
	if loginUser.ID != 1 {
		return errors.New("无权操作")
	}

	// 检测用户名是否被占用
	has, err := model.ExistUserByName(req.Username)
	if err != nil {
		return err
	}
	if has {
		return errors.New("用户名已被占用")
	}

	user := model.User{
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
	}

	if _, err := user.Insert(); err != nil {
		return err
	}

	return "添加成功"
}

// UpdateUser 修改用户
func (*UserManageController) UpdateUser(c *httpx.Context) any {
	var req struct {
		ID       uint   `json:"id"`
		Username string `json:"username"`
		Email    string `json:"email"`
		Phone    string `json:"phone"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		return err
	}

	loginUser, err := c.GetLoginUser()
	if err != nil {
		return err
	}
	if loginUser.ID != 1 {
		// 当前登录的不是系统用户
		return errors.New("无权操作")
	}

	// 检查表单
	if req.Username == "" {
		return errors.New("请输入用户名")
	}
	if req.Email == "" {
		return errors.New("请输入邮箱")
	}
	if req.Phone == "" {
		return errors.New("请输入电话")
	}

	user, err := model.GetUserByID(req.ID)
	if err != nil {
		return err
	}

	if user == nil {
		return errors.New("用户不存在")
	}
	if user.ID == 1 {
		// 禁止直接操作系统用户
		return errors.New("无权操作")
	}

	user.Username = req.Username
	user.Email = req.Email
	_, err = user.Update()
	if err != nil {
		return err
	}

	return "修改成功"
}

// RestPassword 重置用户密码
func (*UserManageController) RestPassword(ctx *httpx.Context) any {
	var id uint
	err := ctx.Query("id", &id)
	if err != nil {
		return errors.New("参数错误")
	}
	loginUser, err := ctx.GetLoginUser()
	if err != nil {
		return err
	}
	if loginUser.ID != 1 {
		// 当前登录的不是系统用户
		return errors.New("无权操作")
	}

	user, err := model.GetUserByID(id)
	if err != nil {
		return err
	}
	if user == nil {
		return errors.New("用户不存在")
	}
	password, err := tools.RandomStr(16)
	if err != nil {
		return err
	}
	_, err = user.UpdatePassword(password)
	if err != nil {
		return err
	}
	return gin.H{
		"password": password,
	}
}

// DeleteUser 删除用户
func (*UserManageController) DeleteUser(ctx *httpx.Context) any {
	// 解析URL参数
	var id uint
	err := ctx.Query("id", &id)
	if err != nil {
		return errors.New("参数错误")
	}
	loginUser, err := ctx.GetLoginUser()
	if err != nil {
		return err
	}
	if loginUser.ID != 1 {
		// 当前登录的不是系统用户
		return errors.New("无权操作")
	}
	// 查询用户
	user, err := model.GetUserByID(id)
	if err != nil {
		return err
	}
	if user == nil {
		return errors.New("用户不存在")
	}
	if user.ID == 1 {
		// 禁止直接操作系统用户
		return errors.New("无权操作")
	}
	// 删除用户
	_, err = user.Delete()
	if err != nil {
		return err
	}
	return "删除成功"
}
